package com.bugstack.domain.award.service;

import com.bugstack.domain.award.event.SendAwardMessageEvent;
import com.bugstack.domain.award.model.aggregate.UserAwardRecordAggregate;
import com.bugstack.domain.award.model.entity.DistributeAwardEntity;
import com.bugstack.domain.award.model.entity.TaskEntity;
import com.bugstack.domain.award.model.entity.UserAwardRecordEntity;
import com.bugstack.domain.award.model.valobj.TaskStateVO;
import com.bugstack.domain.award.repository.IAwardRepository;
import com.bugstack.domain.award.rule.IDistributeAward;
import com.bugstack.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: czb
 * @Description: 奖励服务
 * @DateTime: 2025/3/27 21:58
 **/
@Slf4j
@Service
public class AwardService implements IAwardService {

    private final IAwardRepository awardRepository;
    private final SendAwardMessageEvent sendAwardMessageEvent;
    private final Map<String, IDistributeAward> distributeAwardMap;

    public AwardService(IAwardRepository awardRepository, SendAwardMessageEvent sendAwardMessageEvent, Map<String, IDistributeAward> distributeAwardMap) {
        this.awardRepository = awardRepository;
        this.sendAwardMessageEvent = sendAwardMessageEvent;
        this.distributeAwardMap = distributeAwardMap;
    }

    @Override
    public void saveUserAwardRecord(UserAwardRecordEntity userAwardRecordEntity) {

        //构建消息体
        SendAwardMessageEvent.SendAwardMessage sendAwardMessage = SendAwardMessageEvent.SendAwardMessage.builder()
                .userId(userAwardRecordEntity.getUserId())
                .orderId(userAwardRecordEntity.getOrderId())
                .awardId(userAwardRecordEntity.getAwardId())
                .awardTitle(userAwardRecordEntity.getAwardTitle())
                .awardConfig(userAwardRecordEntity.getAwardConfig())
                .build();

        BaseEvent.EventMessage<SendAwardMessageEvent.SendAwardMessage> sendAwardMessageEventMessage = sendAwardMessageEvent.buildEventMessage(sendAwardMessage);

        //构建任务对象
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setUserId(userAwardRecordEntity.getUserId());
        taskEntity.setState(TaskStateVO.create);
        taskEntity.setTopic(sendAwardMessageEvent.topic());
        taskEntity.setMessage(sendAwardMessageEventMessage);
        taskEntity.setMessageId(sendAwardMessageEventMessage.getId());

        UserAwardRecordAggregate userAwardRecordAggregate = new UserAwardRecordAggregate();
        userAwardRecordAggregate.setUserAwardRecordEntity(userAwardRecordEntity);
        userAwardRecordAggregate.setTaskEntity(taskEntity);

        awardRepository.saveUserAwardRecordAggregate(userAwardRecordAggregate);

    }

    @Override
    public void distributeAward(DistributeAwardEntity distributeAwardEntity) {
        String awardKey = awardRepository.queryAwardKeyByAwardId(distributeAwardEntity.getAwardId());
        if (null == awardKey) {
            log.error("奖品配置不存在 awardId:{}", distributeAwardEntity.getAwardId());
            return;
        }
        IDistributeAward distributeAward = distributeAwardMap.get(awardKey);
        if (null == distributeAward) {
            log.error("没有奖品对应的处理策略 awardId:{}", distributeAwardEntity.getAwardId());
            throw new RuntimeException("分发奖品，奖品" + awardKey + "对应的服务不存在");
        }
        distributeAward.giveOutPrizes(distributeAwardEntity);
    }
}
